package com.zrx.service.impl;

import com.alibaba.excel.util.BeanMapUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrx.constants.SystemConstants;
import com.zrx.domain.ResponseResult;
import com.zrx.domain.dto.TagListDto;
import com.zrx.domain.entity.Article;
import com.zrx.domain.entity.Category;
import com.zrx.domain.vo.CategoryListVo;
import com.zrx.domain.vo.CategoryVo;
import com.zrx.domain.vo.PageVo;
import com.zrx.mapper.CategoryMapper;
import com.zrx.service.ArticleService;
import com.zrx.service.CategoryService;
import com.zrx.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2022-09-16 09:08:57
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private ArticleService articleService;
    /**
     * 文章类别
     * @return
     */
    @Override
    public ResponseResult getCategoryList() {

        //查询 文字表 状态已发的文章
        LambdaQueryWrapper<Article> articleWrapper = new LambdaQueryWrapper<>();
        articleWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Article> articleList = articleService.list(articleWrapper);
        //获取文章的分类id 去重
        Set<Long> collectIds = articleList.stream()
                .map(article -> article.getCategoryId())
                .collect(Collectors.toSet());

        //查询分类表
        List<Category> categories = listByIds(collectIds);
        //过滤
        categories.stream()
                .filter(category -> SystemConstants.STATUS_NORMAL.equals(category.getStatus()))
        .collect(Collectors.toList());
        //封装vo
        List<CategoryListVo> categoryVos = BeanCopyUtils.copyBeanList(categories, CategoryListVo.class);


        return ResponseResult.okResult(categoryVos);
    }

    @Override
    public ResponseResult listAllCategory() {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getStatus,SystemConstants.NORMAL);
        List<Category> list = list(queryWrapper);
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(list, CategoryVo.class);

        return ResponseResult.okResult(categoryVos);
    }

    /**
     * 分页查询分类
     * @param pageNum
     * @param pageSize
     * @param category
     * @return
     */
    @Override
    public ResponseResult<PageVo> pageCategoryList(Integer pageNum, Integer pageSize, Category category) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(StringUtils.hasText(category.getName()),Category::getName, category.getName());
        queryWrapper.eq(Objects.nonNull(category.getStatus()),Category::getStatus, category.getStatus());

        Page<Category> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        return ResponseResult.okResult(new PageVo(page.getRecords(),page.getTotal()));
    }
}

